package com.cspg.design.ui.document;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.cspg.design.R;
import com.cspg.design.utils.AppManager;
import com.cspg.design.widget.ToastUtils;
import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnPageChangeListener;

import java.io.File;

import static java.lang.String.format;

public class PdfReaderActivity extends AppCompatActivity implements OnPageChangeListener {

    public static final String SAMPLE_FILE = "sample.pdf";

    public static final String ABOUT_FILE = "about.pdf";
    private PDFView pdfView;

    String pdfName = SAMPLE_FILE;
    Integer pageNumber = 1;
    private  File mFile;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf_reader);
        pdfView = findViewById(R.id.pdfView);
        AppManager.getAppManager().addActivity(this);
        String filePath = getIntent().getStringExtra("file");
        if (filePath!= null)
        {
            mFile = new File(filePath);
            if (!mFile.exists())
            {
                ToastUtils.toast(this,"文件不存在");
                return;
            }
        }
        else {
            ToastUtils.toast(this,"文件不存在");
            return;
        }
        display(pdfName,true);
    }

    private void display(String assetFileName, boolean jumpToFirstPage) {
        if (jumpToFirstPage) pageNumber = 1;
        setTitle(pdfName = assetFileName);

        pdfView.fromFile(mFile)
                .defaultPage(pageNumber)
                .onPageChange(this)
                .load();
//        pdfView.fromAsset(assetFileName)
//                .defaultPage(pageNumber)
//                .onPageChange(this)
//                .load();
    }

    @Override
    public void onPageChanged(int page, int pageCount) {
        pageNumber = page;
        setTitle(format("%s %s / %s", pdfName, page, pageCount));
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}